package com.koron.sinoprobe.web.engineering.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.*;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.dto.SpWarrantyAcceptDTO;
import com.koron.sinoprobe.web.engineering.bean.po.SpWarrantyAccept;
import com.koron.sinoprobe.web.engineering.bean.query.SpWarrantyAcceptQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpWarrantyAcceptVO;
import com.koron.sinoprobe.web.engineering.service.SpWarrantyAcceptService;
import com.koron.sinoprobe.web.engineering.workflow.StaffHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 质保期满验收(SpWarrantyAccept)表控制层
 *
 * @author jzp
 * @since 2022-03-03 09:21:35
 */
@Api(tags = "质保期满验收接口")
@RestController
@RequestMapping("sp/warrantyAccept")
public class SpWarrantyAcceptController implements WorkflowController, WorkflowPluginController {
    /**
     * 服务对象
     */
    @Autowired
    private SpWarrantyAcceptService spWarrantyAcceptService;
    @Autowired
    private StaffHelper staffHelper;
    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<SpWarrantyAcceptDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "getById", SpWarrantyAcceptDTO.class, id));
    }


    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo<SpWarrantyAcceptVO>> page(PageQuery pageQuery, SpWarrantyAcceptQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "page", PageInfo.class, pageQuery, query));
    }


    @ApiOperation(value = "保存或更新")
    @PostMapping("save")
    public Response<String> save(@RequestBody SpWarrantyAcceptDTO spWarrantyAcceptDTO) {
        ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "saveOrUpdateDto", Boolean.class, spWarrantyAcceptDTO);
        return Response.ok(spWarrantyAcceptDTO.getId());
    }

    @ApiOperation(value = "提交")
    @PostMapping("submit")
    public Response<String> submit(@RequestBody SpWarrantyAcceptDTO spWarrantyAcceptDTO) {
        ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "submit", Boolean.class, spWarrantyAcceptDTO);
        return Response.ok(spWarrantyAcceptDTO.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    /**
     * 工作溜
     */
    @Override
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        // 提交
        if (redirectQuery.getOperation() == 32) {
            // 如果是结束
            if (redirectQuery.getCurNodeCode().equals("END")) {
                ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "end", Boolean.class, redirectQuery.getDataId());
            } else {
                // 否则为提交
                ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "updateStatus", Boolean.class, redirectQuery.getDataId(), Constants.AppceptStatus.WAIT_REVIEW);
            }
        } else if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 如果是驳回或撤回，回到草稿状态
            ADOConnection.runTask(getCurrDs(), spWarrantyAcceptService, "updateStatus", Boolean.class, redirectQuery.getDataId(), Constants.PackageStatus.DRAFT);
        }
        return Response.success();
    }

    /**
     * 取交集
     */
    @Override
    public Response<List<WorkflowUser>> submitter(@RequestBody SubmitterParam submitterParam) {
        return Response.ok(staffHelper.mixed(submitterParam.getUsers()));
    }

    /**
     * 提交给创建人
     */
    @PostMapping("creator/"+submitter)
    public Response<List<WorkflowUser>> creatorSubmitter(@RequestBody SubmitterParam submitterParam) {
        // 1. 获取编制人信息
        SpWarrantyAccept spWarrantyAccept = JSONObject.parseObject(submitterParam.getFormVariables(), SpWarrantyAccept.class);
        // 编制人账号
        return Response.ok(Collections.singletonList(new WorkflowUser(spWarrantyAccept.getCreateBy(), spWarrantyAccept.getCreateByName())));
    }

    /**
     * 提交给创建人所在部门
     */
    @PostMapping("creatorOrg/"+submitter)
    public Response<List<WorkflowUser>> creatorOrgSubmitter(@RequestBody SubmitterParam submitterParam) {
        // 1. 获取编制人信息
        SpWarrantyAccept spWarrantyAccept = JSONObject.parseObject(submitterParam.getFormVariables(), SpWarrantyAccept.class);
        // 2. 获取编制人所在部门的人
        List<WorkflowUser> orgByAccount = staffHelper.getOrgByAccount(spWarrantyAccept.getCreateBy());
        List<WorkflowUser> createUser = staffHelper.getPostUserQuery(orgByAccount, null);

        // 3. 岗位取并集
        List<WorkflowUser> workflowUsers2 = submitterParam.getUsers().stream().filter(e -> e.getType() == 2).collect(Collectors.toList());
        List<WorkflowUser> gwUser = staffHelper.getPostUserQuery(workflowUsers2, null);

        // 4. 取交集
        return Response.ok(staffHelper.intersectionUser(Arrays.asList(createUser, gwUser)));
    }

    @Override
    public Response<List<WorkflowUser>> handler(@RequestBody SubmitterParam submitterParam) {
        return Response.ok();
    }

}

